import {applyMiddleware, createStore}  from 'redux';
 
// import logger from 'redux-logger';
import thunk from 'redux-thunk';
 

import {adminlogin,showadmin,addadmin,deleteadmin,getadminId,updateadmin} from '../apis/admin.js'


const store = createStore(reducer,applyMiddleware(thunk));

export const listdata =({list,totalNum,currentPage,pageSize})=>({
    type:"setdata",
        list,
        totalNum,
        currentPage,
        pageSize,
});
export const loadingstate =({loading})=>({
    type:"changeloading",
    loading,
})

function reducer(state={
    list:[],
    totalNum:0,
    currentPage:1,
    pageSize:1,
    loading:false,
},action) {
    switch (action.type) {
        case "setdata":
            return {
                list:action.list,
                totalNum:action.totalNum,
                currentPage:action.currentPage,
                pageSize:action.pageSize,
            };
        case "changeloading":
            return {
                ...state,
                loading:action.loading,
            }
    
        default:return state;
    }
}

export const getShowadminAC = (params={pageSize: 10, pageNumber: 1})=>{
    return async (dispatch,getState)=>{
        try {
            dispatch(loadingstate(true));
            const { pageSize, totalNum, totalPages, currentPage, data,}= await showadmin(params);
            dispatch(listdata({list:data,totalNum:totalNum,pageSize:pageSize,totalPages:totalPages, currentPage:currentPage,}))
            dispatch(loadingstate(false));
        } catch (error) {
            dispatch(loadingstate(false))
        }
    }
}


export default store;